package com.yang.job.infra.gatewayimpl.repository;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.job.client.dto.common.PageDTO;
import com.yang.job.domain.gateway.repository.IJobModelRepository;
import com.yang.job.domain.gateway.repository.request.JobModelQueryCondition;
import com.yang.job.domain.model.YangJobModel;
import com.yang.job.infra.data.YangJobData;
import com.yang.job.infra.data.mapper.YangJobDataMapper;
import com.yang.job.infra.gatewayimpl.repository.convertor.YangJobModelConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Repository
public class JobModelRepository implements IJobModelRepository {
    @Autowired
    private YangJobDataMapper yangJobDataMapper;

    @Resource
    private YangJobModelConvertor yangJobModelConvertor;

    @Override
    public boolean saveYangJobModel(YangJobModel yangJobModel) {
        yangJobModel.setCreateTime(new Date());
        yangJobModel.setUpdateTime(new Date());
        yangJobModel.setEnable(1);
        YangJobData yangJobData = yangJobModelConvertor.convert2Data(yangJobModel);
        int insert = yangJobDataMapper.insert(yangJobData);
        if (insert > 0) {
            yangJobModel.setJobId(yangJobData.getJobId());
            return true;
        }
        return false;
    }

    @Override
    public boolean updateYangJobModel(YangJobModel yangJobModel) {
        yangJobModel.setUpdateTime(new Date());
        YangJobData yangJobData = yangJobModelConvertor.convert2Data(yangJobModel);
        return yangJobDataMapper.updateById(yangJobData) > 0;
    }

    @Override
    public YangJobModel getYangJobModelById(Integer id) {
        YangJobData yangJobData = yangJobDataMapper.selectById(id);
        return yangJobModelConvertor.convert2Model(yangJobData);
    }

    @Override
    public List<YangJobModel> getOpenYangJobModelList() {
        LambdaQueryWrapper<YangJobData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YangJobData::getOpen, 1)
                .eq(YangJobData::getEnable, 1);
        List<YangJobData> yangJobDataList = yangJobDataMapper.selectList(queryWrapper);
        return yangJobDataList.stream()
                .map(yangJobModelConvertor::convert2Model)
                .collect(Collectors.toList());
    }

    @Override
    public PageDTO<YangJobModel> queryYangJobModelPage(JobModelQueryCondition jobModelQueryCondition) {
        Page<YangJobData> page = new Page<>(jobModelQueryCondition.getPageNo(), jobModelQueryCondition.getPageSize());
        LambdaQueryWrapper<YangJobData> queryWrapper = new LambdaQueryWrapper<>();
        if (jobModelQueryCondition.getOpen() != null) {
            queryWrapper.eq(YangJobData::getOpen, jobModelQueryCondition.getOpen());
        }
        queryWrapper.eq(YangJobData::getEnable, 1);
        page = yangJobDataMapper.selectPage(page, queryWrapper);

        PageDTO<YangJobModel> pageDTO = new PageDTO<>();
        pageDTO.setPageNo(jobModelQueryCondition.getPageNo());
        pageDTO.setPageSize(jobModelQueryCondition.getPageSize());
        pageDTO.setPages(page.getPages());
        pageDTO.setTotal(page.getTotal());
        List<YangJobModel> dataList = page.getRecords().stream()
                .map(yangJobModelConvertor::convert2Model)
                .collect(Collectors.toList());
        pageDTO.setDataList(dataList);
        return pageDTO;
    }

    @Override
    public boolean deleteJobModel(Integer id) {
        LambdaUpdateWrapper<YangJobData> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(YangJobData::getJobId, id)
                .set(YangJobData::getEnable, 0)
                .set(YangJobData::getUpdateTime, new Date());
        return yangJobDataMapper.update(null, updateWrapper) > 0;
    }
}

